package com.suse.lhw.hms.foodserver.service.Impl;

import com.suse.lhw.hms.foodserver.constant.FoodModelStatusConst;
import com.suse.lhw.hms.foodserver.dao.FFoodIfoMapper;
import com.suse.lhw.hms.foodserver.dao.FFoodmodelToFFoodmodelUnitMapper;
import com.suse.lhw.hms.foodserver.dao.FUnitMapper;
import com.suse.lhw.hms.foodserver.domin.FFoodIfo;
import com.suse.lhw.hms.foodserver.domin.FFoodmodelToFFoodmodelUnit;
import com.suse.lhw.hms.foodserver.domin.FUnit;
import com.suse.lhw.hms.foodserver.pojo.input_POJO.ModelUnitAndIfo;
import com.suse.lhw.hms.foodserver.pojo.input_POJO.addAFoodModel;
import com.suse.lhw.hms.foodserver.pojo.input_POJO.modelAndUnits;
import com.suse.lhw.hms.foodserver.pojo.output_POJO.ofm_selectFoodIfoByModelId;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.suse.lhw.hms.foodserver.domin.FFoodmodel;
import com.suse.lhw.hms.foodserver.dao.FFoodmodelMapper;
import com.suse.lhw.hms.foodserver.service.FFoodmodelService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.foodserver.service.Impl
 * @ClassName: FFoodmodelServiceImpl
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/4/18 19:30
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/18 19:30
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class FFoodmodelServiceImpl implements FFoodmodelService {
    @Resource
    private FFoodmodelMapper fFoodmodelMapper;
    @Resource
    private FFoodmodelToFFoodmodelUnitMapper fFoodmodelToFFoodmodelUnitMapper;
    @Resource
    private FUnitMapper unitMapper;
    @Resource
    private FFoodIfoMapper fFoodIfoMapper;


    @Override
    public String addAFoodModel(addAFoodModel pojo, List<FUnit> list) {
        /**
         * @Author:Hongwei Li
         * @Description:添加一个食品模型，同时也生成食品信息，食品单位以及对食品单位的映射
         * @Date:8:59 2020/4/26
         * @Param:[model, pojo, list]
         * @Return:java.lang.String
         **/
        System.out.println(pojo.toString());
        if (list.isEmpty())
            System.out.println("这个单位传过来是空的");
        else
            System.out.println("这个单位传过来不是空的");
        for (FUnit addAFoodModel_unit : list) {
            System.out.println(addAFoodModel_unit.getUnitConsumeCalories() + addAFoodModel_unit.getUnitName() + "\n");
        }

        //添加食品的信息
        FFoodIfo fFoodIfo = new FFoodIfo(pojo.getIfodescroption(), pojo.getCarbohydrate(), pojo.getProtein(), pojo.getFat(), pojo.getTag(), pojo.getOrtherNutrient());
        System.out.println("生成的foodifo是:" + fFoodIfo.toString());
        int iforesult = fFoodIfoMapper.insert(fFoodIfo);
        if (iforesult == 0) {
            System.out.println("插入foodifo失败");
            return "插入ifo失败";
        } else {
            System.out.println("插入ifo成功，ifo对象是" + fFoodIfo.toString());

        }

        //添加foodmodel字段
        FFoodmodel foodmodel = new FFoodmodel(pojo.getImgurl(), pojo.getFmdescription(), pojo.getName(), FoodModelStatusConst.defaut, fFoodIfo.getId());
        System.out.println("由pojo生成的foodmodel为\n" + foodmodel.toString());
        int modelresult = insertSelective(foodmodel);
        int modelId = 0;
        if (modelresult == 0) {
            System.out.println("插入foodmodel失败");
            return "插入失败";
        } else {
            modelId = foodmodel.getFmId();
            System.out.println("当前foodmodel的id是：" + modelId);
        }


        //添加食品单位字段
        List<Integer> unitList = new ArrayList<Integer>();
        int unitId = 0;
        for (FUnit unit : list) {
            FUnit fm_unit = new FUnit(unit.getUnitName(), unit.getUnitConsumeCalories());
            System.out.println("由pojo生成的unit为\n" + foodmodel.toString());
            int unitresult = unitMapper.insert(fm_unit);
            if (modelresult == 0) {
                System.out.println("插入foodmodel失败");
                return "插入插入foodmodel失败失败";
            } else {
                unitId = fm_unit.getFUnitId();
                unitList.add(unitId);
                System.out.println("生成的unitId是" + unitId);
            }
        }

        //添加模型对单位的映射字段
        for (Integer integer : unitList) {
            FFoodmodelToFFoodmodelUnit mu_map = new FFoodmodelToFFoodmodelUnit(modelId, integer);
            int mumapresutl = fFoodmodelToFFoodmodelUnitMapper.insert(mu_map);
            if (mumapresutl == 0) {
                System.out.println("插入FFoodmodelTOFFoodmodelUnit失败");
                return "插入失败";
            } else {
                System.out.println("插入FFoodmodelTOFFoodmodelUnit成功");
            }
        }
        return "添加成功了哦，（づ￣3￣）づ╭❤～";
    }


    @Override
    public int deleteByPrimaryKey(Integer fmId) {
        return fFoodmodelMapper.deleteByPrimaryKey(fmId);
    }

    @Override
    public int insert(FFoodmodel record) {
        return fFoodmodelMapper.insert(record);
    }

    @Override
    public int insertSelective(FFoodmodel record) {
        return fFoodmodelMapper.insertSelective(record);
    }

    @Override
    public FFoodmodel selectByPrimaryKey(Integer fmId) {
        return fFoodmodelMapper.selectByPrimaryKey(fmId);
    }

    @Override
    public int updateByPrimaryKeySelective(FFoodmodel record) {
        return fFoodmodelMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(FFoodmodel record) {
        return fFoodmodelMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<FFoodmodel> selectFFoodmodelsByStatus(int status) {
        return fFoodmodelMapper.selectFoodModelsByStatus(status);
    }

    @Override
    public String deleteADefaultFoodModel(Integer foodmodelId) {
        /**
         * @Author:Hongwei Li
         * @Description:删除一个默认的食品模型，顺便删除食品信息和食品单位以及食品单位映射
         * @Date:10:10 2020/4/26
         * @Param:[foodmodelId]
         * @Return:java.lang.String
         **/
        //删除食品模型
        FFoodmodel model = fFoodmodelMapper.selectByPrimaryKey(foodmodelId);
        int modelId = model.getFmId();
        System.out.println("食品模型为" + model.toString());
        int modelresutl = fFoodmodelMapper.deleteByPrimaryKey(foodmodelId);
        if (modelresutl == 0) {
            System.out.println("删除食品模型失败");
            return "删除失败";
        }
        //找出对应的食品信息并删除
        FFoodIfo ifoId = fFoodIfoMapper.selectByPrimaryKey(model.getFmsgId());
        int iforesult = fFoodIfoMapper.deleteByPrimaryKey(ifoId.getId());
        if (iforesult == 0) {
            System.out.println("删除食品信息失败");
            return "删除食品信息失败";
        }

        //找出所有的食品单位并删除
        List<FFoodmodelToFFoodmodelUnit> unitmap = fFoodmodelToFFoodmodelUnitMapper.selectByFmId(model.getFmId());
        List<Integer> unitIdList = new ArrayList<Integer>();
        for (FFoodmodelToFFoodmodelUnit fFoodmodelToFFoodmodelUnit : unitmap) {
            unitIdList.add(fFoodmodelToFFoodmodelUnit.getId());
            if (unitMapper.deleteByPrimaryKey(fFoodmodelToFFoodmodelUnit.getFUnit()) == 0) {
                System.out.println("食品单位删除失败！，自己反省一下");
                return "食品单位删除失败！";
            }

        }

        //删除食品单位映射
        for (Integer integer : unitIdList) {
            if (fFoodmodelToFFoodmodelUnitMapper.deleteByPrimaryKey(integer) == 0) {
                System.out.println("删除食品单位映射失败！反省");
                return "食品单位映射失败！垃圾玩意儿！";
            }
        }
        return "满门抄斩成功！";
    }

    @Override
    public List<ofm_selectFoodIfoByModelId> selectAllModelByPackageId(int packageId) {
        //通过ptm映射找出对应的食品模型
        List<FFoodmodel> list = fFoodmodelMapper.selectAllByPackageId(packageId);
        return null;
    }


    @Override
    public String addAUnuploadedFoodModel(addAFoodModel pojo, List<FUnit> list) {
        /**
         * @Author:Hongwei Li
         * @Description:添加一个食品模型，同时也生成食品信息，食品单位以及对食品单位的映射
         * @Date:8:59 2020/4/26
         * @Param:[model, pojo, list]
         * @Return:java.lang.String
         **/
        String userId = "aaa";
        System.out.println(pojo.toString());
        if (list.isEmpty())
            System.out.println("这个单位传过来是空的");
        else
            System.out.println("这个单位传过来不是空的");
        for (FUnit addAFoodModel_unit : list) {
            System.out.println(addAFoodModel_unit.getUnitConsumeCalories() + addAFoodModel_unit.getUnitName() + "\n");
        }

        //添加食品的信息
        FFoodIfo fFoodIfo = new FFoodIfo(pojo.getIfodescroption(), pojo.getCarbohydrate(), pojo.getProtein(), pojo.getFat(), pojo.getTag(), pojo.getOrtherNutrient());
        System.out.println("生成的foodifo是:" + fFoodIfo.toString());
        int iforesult = fFoodIfoMapper.insert(fFoodIfo);
        if (iforesult == 0) {
            System.out.println("插入foodifo失败");
            return "插入ifo失败";
        } else {
            System.out.println("插入ifo成功，ifo对象是" + fFoodIfo.toString());

        }

        //添加foodmodel字段
        FFoodmodel foodmodel = new FFoodmodel(pojo.getImgurl(), pojo.getFmdescription(), pojo.getName(), FoodModelStatusConst.unupload, fFoodIfo.getId());
        foodmodel.setUserId(userId);
        System.out.println("由pojo生成的foodmodel为\n" + foodmodel.toString());
        int modelresult = fFoodmodelMapper.insertSelective(foodmodel);
        int modelId = 0;
        if (modelresult == 0) {
            System.out.println("插入foodmodel失败");
            return "插入失败";
        } else {
            modelId = foodmodel.getFmId();
            System.out.println("当前foodmodel的id是：" + modelId);
        }


        //添加食品单位字段
        List<Integer> unitList = new ArrayList<Integer>();
        int unitId = 0;
        for (FUnit unit : list) {
            FUnit fm_unit = new FUnit(unit.getUnitName(), unit.getUnitConsumeCalories());
            System.out.println("由pojo生成的unit为\n" + foodmodel.toString());
            int unitresult = unitMapper.insert(fm_unit);
            if (modelresult == 0) {
                System.out.println("插入foodmodel失败");
                return "插入插入foodmodel失败失败";
            } else {
                unitId = fm_unit.getFUnitId();
                unitList.add(unitId);
                System.out.println("生成的unitId是" + unitId);
            }
        }

        //添加模型对单位的映射字段
        for (Integer integer : unitList) {
            FFoodmodelToFFoodmodelUnit mu_map = new FFoodmodelToFFoodmodelUnit(modelId, integer);
            int mumapresutl = fFoodmodelToFFoodmodelUnitMapper.insert(mu_map);
            if (mumapresutl == 0) {
                System.out.println("插入FFoodmodelTOFFoodmodelUnit失败");
                return "插入失败";
            } else {
                System.out.println("插入FFoodmodelTOFFoodmodelUnit成功");
            }
        }
        return "添加成功了哦，（づ￣3￣）づ╭❤～";
    }

    @Override
    public List<modelAndUnits> selectByNameStatusUserid(String name, int status, String userId) {
        //通过传入的名称，状态，用户id查找相应的模型以及他的单位们
        //其中的参数可能有缺省，直接通过动态sql拼接,如果状态指向是用户自定义的模型则需要使用userId
        List<modelAndUnits> resutl = new ArrayList<modelAndUnits>();
        if (name == null && status == -1)
            return null;
        if (status == 0) {
            //先找到modle
            List<FFoodmodel> fFoodmodels = fFoodmodelMapper.selectByNameStatus(name, status);
            //在根据modle的id找到所有的units
            for (FFoodmodel fFoodmodel : fFoodmodels) {
                List<FUnit> units = unitMapper.selectAllUnitByModelId(fFoodmodel.getFmId());
                //封装
                modelAndUnits modelAndUnits = new modelAndUnits(fFoodmodel.getFmId(), fFoodmodel.getSatus(), fFoodmodel.getName(), fFoodmodel.getFmCategory(), fFoodmodel.getImgurl(), fFoodmodel.getFmsgId(), fFoodmodel.getDescription(), fFoodmodel.getUserId(), units);
                System.out.println("the modelAAndUnits are" + modelAndUnits.toString());
                resutl.add(new modelAndUnits(fFoodmodel.getFmId(), fFoodmodel.getSatus(), fFoodmodel.getName(), fFoodmodel.getFmCategory(), fFoodmodel.getImgurl(), fFoodmodel.getFmsgId(), fFoodmodel.getDescription(), fFoodmodel.getUserId(), units));
            }
        } else {
            //需要使用userid查找对应的用户自定义食品
            List<FFoodmodel> fFoodmodels = fFoodmodelMapper.selectByNameStatusUserId(name, status, userId);
            for (FFoodmodel fFoodmodel : fFoodmodels) {
                List<FUnit> units = unitMapper.selectAllUnitByModelId(fFoodmodel.getFmId());
                resutl.add(new modelAndUnits(fFoodmodel.getFmId(), fFoodmodel.getSatus(), fFoodmodel.getName(), fFoodmodel.getFmCategory(), fFoodmodel.getImgurl(), fFoodmodel.getFmsgId(), fFoodmodel.getDescription(), fFoodmodel.getUserId(), units));
            }
        }
        return resutl;
    }

    @Override
    public ModelUnitAndIfo selectModelAndUnitsById(int id) {
        FFoodmodel fFoodmodel = fFoodmodelMapper.selectByPrimaryKey(id);
        List<FUnit> units = unitMapper.selectAllUnitByModelId(id);
        FFoodIfo afFoodIfo = fFoodIfoMapper.selectByPrimaryKey(fFoodmodel.getFmsgId()
        );
        ModelUnitAndIfo modelAndUnits = new ModelUnitAndIfo(id, fFoodmodel.getName(), fFoodmodel.getImgurl(), fFoodmodel.getDescription(), afFoodIfo.getDescroption(), afFoodIfo.getCarbohydrate(), afFoodIfo.getProtein(), afFoodIfo.getFat(), afFoodIfo.getTag(), afFoodIfo.getOrtherNutrient(), units);
        System.out.println("modelanduints is" + modelAndUnits);
        return modelAndUnits;
    }
}






